package toolNet
{
	import engine.data.Record;
	
	import flash.utils.describeType;

	public class ObjectUtil
	{
		public static function DecodeVo(vo : Object):Object
		{
			
			var jsonObj	:	Object	=	{};
			//获得全全数据xml

			var voXml	:	XML		=	describeType(vo);

			//所有public属性
			var xmllist:XMLList = voXml.child("variable") as XMLList;
			var accessorXmlList:XMLList = voXml.child("accessor") as XMLList;
			

			var xmllistLen:int = xmllist.length();
			var accessorXmllist:int = accessorXmlList.length();
			var alllen:int = accessorXmllist+xmllistLen;
			
			if(vo is Record)
			{
				for(var i:int = xmllistLen;i < alllen;i++)
				{
					xmllist[i] = accessorXmlList[i-xmllistLen];
				}
			}
			
			Decode(xmllist, vo, jsonObj);
			
			//所有get。set属性
//			Decode(voXml.child("accessor"), vo, jsonObj);
			
			return jsonObj;
		}
		
		public static function DecodeArr(arr:Array):Object
		{
			var jsonObj:Object = {};
			var vo:Object = {};
			for (var i:int = 0;i< arr.length;i++)
			{
				vo["col"+i] = {};
				if((arr[i] as Array)!= null)
				{
//					var xml:XML = <node name = {arr[i]}></node>;
					for(var j:int = 0; j< arr[i].length;j++)
					{
						vo["col"+i]["row"+j] = arr[i][j];	
					}
				}
			}
			return vo;
		}
		
		/**
		 *	解析成obj 
		 * @param sourceXml		需要解析的xmlList
		 * @param sourceVo		需要解析的vo
		 * @param targetObj		解析后的obj
		 * @return 
		 * 
		 */
		private static function Decode(sourceXml : XMLList, sourceVo : Object, targetObj : Object = null):*
		{
			var hasChild	:	Boolean	=	false;

			if(targetObj	==	null)
			{
				var jsonObj :	Object	=	{};
			}else
			{
				jsonObj					=	targetObj;
			}
			if(sourceXml.length() == 0 )
			{
				for(var p : String in sourceVo)
				{
					var type:int = parametersType(sourceVo[p]);
					if(type == 1)
					{
						jsonObj[p]	=	sourceVo[p];
						
					}
					else if(type == 2)
					{
						hasChild	=	true;
						jsonObj[p]	=	DecodeVo(sourceVo[p]);
					}else
					{
						hasChild	=	true;
						jsonObj[p]	=	DecodeVo(sourceVo[p]);
					}
				}
			}else
			{
				for each(var xml : XML in sourceXml)
				{
					var type1:int = parametersType(xml.@type);
					if(type1 == 1)
					{
						jsonObj[xml.@name]	=	sourceVo[xml.@name];
						
					}
					else if(type1 == 2)
					{
						hasChild	=	true;
						if(String(xml.@name).indexOf("recArr") != -1)
						{
							jsonObj[xml.@name]	=	DecodeVo(DecodeArr(sourceVo[xml.@name]));
						}else
						{
							jsonObj[xml.@name]	=	DecodeVo(sourceVo[xml.@name]);
						}
						
//						jsonObj[xml.@name]	=	DecodeVo(DecodeArr(sourceVo[xml.@name]));
//						jsonObj[xml.@name]	=	DecodeVo(sourceVo[xml.@name]);
					}
					else
					{
						hasChild	=	true;
						jsonObj[xml.@name]	=	DecodeVo(sourceVo[xml.@name]);
					}
				}
			}
			
			if(hasChild)
			{
				jsonObj["pType"]	=	sourceVo.toString();
			}
			
			return jsonObj;
		}
		
		/**
		 *	获得data类型 
		 * @param	data
		 * @return	1,0,-1,2 / 基本类型，object， vo ,array	
		 * 
		 */
		private static function parametersType(data : *):int
		{
			var type : String	=	Object(data).valueOf();
			type				=	type.toLocaleLowerCase();
			
			if(type.search("object") != -1)
			{ 
				type.indexOf("object") != type.lastIndexOf("object")
				{
					return -1;
				}
				return 0;
			}
			if(type.search("array") != -1)
			{
				return 2;
			}
			return 1;
		}
	}
}